package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Algorithm;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import db.DBReader;

public class ReaderService extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setCharacterEncoding("UTF-8");
		DBReader reader = new DBReader();
		String method = request.getParameter("method");
		if (method.equals("getAllAlgs")) {
			getAllAlg(request, response);
		} else if (method.equals("getAlgInfo")) {
			getAlgInfo(request, response);
		} else if (method.equals("getFeaturesByAlgID")) {
			getFeaturesByAlgID(request, response);
		} else if (method.equals("getFeatureByID")) {
			getFeatureByID(request, response);
		} else {
			System.err.println("Unknown method.");
		}
		try {
			reader.close();
		} catch (Exception ex) {
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setCharacterEncoding("UTF-8");
		doGet(request, response);
	}

	/**
	 * Get all the algorithm list.
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getAllAlg(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		DBReader reader = new DBReader();
		List<Algorithm> algs = reader.getAllAlg();
		String json = new Gson().toJson(algs);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
		try {
			reader.close();
		} catch (Exception ex) {
		}
	}

	/**
	 * Get the details of the algorithm.
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getAlgInfo(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		String algID = request.getParameter("id");
		DBReader reader = new DBReader();
		List<String> algDesc = reader.getDescriptionByAlgID(algID);
		JsonObject jobj = new JsonObject();
		
		jobj.addProperty("Description", algDesc.get(0).replaceAll("\n", "<br/>"));
		jobj.addProperty("Input", algDesc.get(1).replaceAll("\n", "<br/>"));
		jobj.addProperty("Output", algDesc.get(2).replaceAll("\n", "<br/>"));
		PrintWriter out = response.getWriter();
		out.print(jobj.toString());
		out.close();
		try {
			reader.close();
		} catch (Exception ex) {
		}
	}

	/**
	 * Get the features by algorithm ID.
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getFeaturesByAlgID(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		String algID = request.getParameter("id");
		DBReader reader = new DBReader();
		List<String> featureList = reader.getFeaturesByAlgID(algID);
		String json = new Gson().toJson(featureList);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
		try {
			reader.close();
		} catch (Exception ex) {
		}
	}

	/**
	 * Get the feature deatail by its ID.
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getFeatureByID(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		String featureID = request.getParameter("id");
		DBReader reader = new DBReader();
		Map<String, String> featureDetails = reader.getFeatureByID(featureID);
		String json = new Gson().toJson(featureDetails);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
		try {
			reader.close();
		} catch (Exception ex) {
		}
	}
}
